<template>
	<div>
		<AplidForm ref="formRef" v-bind="ExtendFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
	</div>
</template>

<script setup lang="ts">
import { ExtendFormConfig } from '../../config'
import { getAreaObj } from '@/utils/util'
import { cloneDeep } from 'lodash-es'
import { useCommonStore } from '@/stores/common'
import { useOperate } from '@/views/datacenter/hooks/useConfig'
const { setTempParams } = useCommonStore()
const { getValue, getContent } = useOperate(1)
const rowData: any = inject('nowRowData')
let ExtendMod = rowData.value?.data?.dpServiceObjectExtends
let baseExtendData = rowData.value?.baseData?.dpServiceObjectExtends || []
//  联系人信息备份
let baseContactData = rowData.value?.baseData?.dpServiceObjectContacts || []
// 资料信息备份
let baseMaterData = rowData.value?.baseData?.dpServiceObjectMaterials || []

const emit = defineEmits(['submit'])
const contactData = rowData.value?.data?.dpServiceObjectContacts || []
const formModel = ref({
	...cloneDeep(ExtendMod),
	dp_service_object_contact: contactData,
	dp_service_object_material: rowData.value?.data?.dpServiceObjectMaterials,
})
const formRef = ref(null)
const onSubmit = (form, done) => {
	const { tempNowObj } = storeToRefs(useCommonStore())
	if (form.dp_service_object_contact.length == 0) {
		ElMessage({
			type: 'success',
			message: '联系人信息至少添加一人',
		})
    done()
		return
	}
	const params = {
		dpServiceObjectMaterialPOs: {},
		dpServiceObjectExtendPO: {
			...form,
			dp_service_object_material: undefined,
			dp_service_object_contact: undefined,
			idCard: tempNowObj.value.idCard,
			code: 1003,
		},
		dpServiceObjectContactPOs: {},
	}
	if (form.dp_service_object_material) {
		params.dpServiceObjectMaterialPOs = form.dp_service_object_material.map((item) => {
			return {
				...item,
				idCard: tempNowObj.value.idCard,
				code: 1004,
			}
		})
	}
	if (form.dp_service_object_contact) {
		params.dpServiceObjectContactPOs = form.dp_service_object_contact.map((item) => {
      if(item.contactCardId) item.contactCardId = item.contactCardId.toUpperCase()
			const areaObj: any = getAreaObj(item.contactAreas, 'contact')
			const areaNameObj: any = getAreaObj(item.contact_areas_name, 'contact')
			const data = {
				...item,
				...areaObj,
				...areaNameObj,
				idCard: tempNowObj.value.idCard,
				code: 1002,
			}
			return data
		})
	}
	// 处理变更字段
	let changeColumnsBase = []
	ExtendFormConfig.column
		.map((item) => {
			if (item.changeField && item.column) {
				changeColumnsBase = item.column
			}
		})
		.filter(Boolean)
	const afterContact: any = params.dpServiceObjectContactPOs

	let changeData = []
	let contactList = baseContactData.length > afterContact.length ? baseContactData : afterContact
	contactList.map((item, index) => {
		changeColumnsBase.map((col) => {
			const baseObj = baseContactData[index] ? baseContactData[index] : {}
			const tempBefore = baseObj[col.propName || col.prop]
			const tempAfter = item[col.propName || col.prop]
			if (tempBefore != tempAfter) {
				changeData.push({
					label: col.label,
					before: getValue(tempBefore, col),
					after: getValue(tempAfter, col),
				})
			}
		})
	})
	const emptyObj = {
		contactAreas: undefined,
		contact_areas_name: undefined,
		code: undefined,
		dp_service_object_material: undefined,
		dp_service_object_contact: undefined,
	}

	let afterMater: any = params.dpServiceObjectMaterialPOs || []

	const extendData = {
		...form,
		dp_service_object_material: undefined,
		dp_service_object_contact: undefined,
		idCard: tempNowObj.value.idCard,
	}
	const changeObj = {
		data: changeData,
		dataChangeContentList: [
			...getContent(baseContactData, afterContact, 1002, '服务对象联系人', emptyObj, 'id'),
			...getContent(baseMaterData, afterMater, 1004, '服务对象资料信息', emptyObj, 'id'),
			{
				changeBefore: {
					...baseExtendData,
					...emptyObj,
					idCard: tempNowObj.value.idCard,
				},
				changeAfter: {
					...extendData,
					...emptyObj,
				},
				changeData: {
					...extendData,
				},
				changeType: '服务对象扩展信息',
				code: 1003,
			},
		],
	}
	setTempParams({
		name: tempNowObj.value.name,
		idCard: tempNowObj.value.idCard,
		nowChange: changeObj,
	})
	emit('submit', changeObj)
}

const onSave = () => {
	formRef.value!.onSubmit()
}
defineExpose({
	onSave,
})
</script>

<style scoped></style>
